/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lenovo.framework.base.ActionBarFragment;
import com.lenovo.framework.base.FBaseActivity;
import com.lenovo.framework.entity.Res;
import com.lenovo.framework.rest.NetworkObserver;
import com.lenovo.framework.util.FPreferenceManager;
import com.lenovo.framework.util.MD5;
import com.lenovo.framework.util.TextHelper;
import com.lenovo.mso2o.R;
import com.lenovo.mso2o.conf.MSConstants;
import com.lenovo.mso2o.entity.ModifyEngineerInfoRequest;
import com.lenovo.mso2o.rest.ServiceGenerator;
import com.lenovo.mso2o.rest.UserAPI;

import butterknife.Bind;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/1/6.
 * Modify Password
 */
public class AppModifyPassFragment extends ActionBarFragment implements View.OnClickListener, ConfirmDialogFragment.DialogClickListener{
    private UserAPI userAPI;
    private MD5 md5_Old;
    private MD5 md5_New;

    @Bind(R.id.tv_appmodify_old)
    EditText etOld_Pass;
    @Bind(R.id.tv_appmodify_new)
    EditText etNew_Pass;
    @Bind(R.id.tv_appmodify_confirm)
    EditText etConfirm_Pass;
    @Bind(R.id.btn_appmodify_confirm)
    Button btnConfirm;

    public static AppModifyPassFragment newInstance() {
        AppModifyPassFragment fragment = new AppModifyPassFragment();

        return fragment;
    }

    public AppModifyPassFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        userAPI = ServiceGenerator.createService(UserAPI.class);
        btnConfirm.setOnClickListener(this);

        return view;
    }


    @Override
    public int getActionBarRightResourceId() {
        return 0;
    }

    @Override
    public void onActionBarRightClick() {

    }

    @Override
    public int provideContentRes() {
        return R.layout.fragment_modifypassword;
    }

    @Override
    protected void onFragmentInVisible(Bundle savedInstanceState) {

    }

    @Override
    protected void onFragmentVisible(Bundle savedInstanceState) {

    }

    private String get2Base64(String origin){
        String once = org.jivesoftware.smack.util.Base64.encodeBytes(origin.getBytes());
        return org.jivesoftware.smack.util.Base64.encodeBytes(once.getBytes());
    }
    @Override
    public void doPositiveClick(String tag) {
        if(tag.equals("change_password")){
            String oldPassword = etOld_Pass.getText().toString();
            String newPassword = etConfirm_Pass.getText().toString();
            String username = FPreferenceManager.getString(MSConstants.ACCOUNT_USERNAME, "");
            ModifyEngineerInfoRequest modifyEngineerInfoRequest = new ModifyEngineerInfoRequest();
            modifyEngineerInfoRequest.setPassword(get2Base64(oldPassword) + ":" + get2Base64(newPassword));
            ((FBaseActivity)getActivity()).processPreview("修改密码", null, false);
            try {
                Observable<Res> accessTokensRxObservable = userAPI.modifyEngineerMsg(username, modifyEngineerInfoRequest);
                accessTokensRxObservable
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .unsubscribeOn(Schedulers.io())
                        .subscribe(new NetworkObserver<Res>() {
                            @Override
                            public void onSuccess(Res res) {
                                ((FBaseActivity)getActivity()).processDismiss();
                                if(res != null && res.getStatus() == 0) {
                                    Toast.makeText(getActivity(), "密码修改成功", Toast.LENGTH_SHORT).show();
                                    getActivity().finish();
                                }else{
                                    Toast.makeText(getActivity(), "密码修改失败，请重试", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onFailure(String tip) {
                                ((FBaseActivity)getActivity()).processDismiss();
                                Toast.makeText(getActivity(), "密码修改失败，请重试", Toast.LENGTH_SHORT).show();
                            }
                        });
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        }
    }

    @Override
    public void doNegativeClick(String tag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_appmodify_confirm:
                //要判断旧密码是否输入正确
                if(TextHelper.isEmptyOrSpaces(etOld_Pass.getText().toString())){
                    Toast.makeText(AppModifyPassFragment.this.getActivity(),getString(R.string.old_password_isempty),Toast.LENGTH_SHORT).show();
                }else if(TextHelper.isEmptyOrSpaces(etNew_Pass.getText().toString())){
                    Toast.makeText(AppModifyPassFragment.this.getActivity(),getString(R.string.new_password_isempty),Toast.LENGTH_SHORT).show();
                }else if(!etNew_Pass.getText().toString().equals(etConfirm_Pass.getText().toString())){
                    Toast.makeText(AppModifyPassFragment.this.getActivity(),getString(R.string.new_password_nosame),Toast.LENGTH_SHORT).show();
                }else if(!FPreferenceManager.getString(MSConstants.ACCOUNT_PASSWORD, "").equals(etOld_Pass.getText().toString())){
                    Toast.makeText(AppModifyPassFragment.this.getActivity(),getString(R.string.old_password_error),Toast.LENGTH_SHORT).show();
                }else{
                    ConfirmDialogFragment cleardialogFragment = ConfirmDialogFragment.newInstance(getActivity().getResources().getString(R.string.change_password_title), getActivity().getResources().getString(R.string.change_password_confirm));
                    cleardialogFragment.setListener(this);
                    cleardialogFragment.show(getFragmentManager(), "change_password");
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
